﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Iit.ITBI.MDSManager
{
    class MasterToSlaveAttributeMapping
    {
        public CustomAttribute MasterAttribute
        {
            get;
            set;
        }

        public CustomAttribute SlaveAttribute
        {
            get;
            set;
        }

        public MDSAttributeSyncState AttributeSyncState
        {
            get;
            set;
        }

        public MasterToSlaveAttributeMapping(DataRow MasterToSlaveAttributeMappingData, MDSQueryVersion QueryVersion)
        {
            if (QueryVersion == MDSQueryVersion.GetAttributeDetailesV1)
            {
                
                this.MasterAttribute = new CustomAttribute(MasterToSlaveAttributeMappingData, SlaveOrMaster.Master, QueryVersion);
                this.SlaveAttribute = new CustomAttribute(MasterToSlaveAttributeMappingData, SlaveOrMaster.Slave, QueryVersion);

                try
                {
                    this.AttributeSyncState = (MDSAttributeSyncState)Convert.ToByte(MasterToSlaveAttributeMappingData["AttributeSyncState"]);

                    switch (this.AttributeSyncState)
                    {
                        case MDSAttributeSyncState.Invalid:
                    
                            throw new InvalidSyncStateException (
                                    string.Format("The attribute mapping for the attribute {0} between the master Entity {1} and the slave Entity {2} is invalid"
                                                  ,this.MasterAttribute.Name
                                                  ,MasterToSlaveAttributeMappingData["MasterEntity_Name"].ToString()
                                                  ,MasterToSlaveAttributeMappingData["SlaveEntity_Name"].ToString()));
                            break;

                        case MDSAttributeSyncState.Unknown:
    
                            throw new UnknownSyncStateException (
                                    string.Format("The attribute mapping for the attribute {0} between the master Entity {1} and the slave Entity {2} is unknown"
                                                ,this.MasterAttribute.Name
                                                ,MasterToSlaveAttributeMappingData["MasterEntity_Name"].ToString()
                                                ,MasterToSlaveAttributeMappingData["SlaveEntity_Name"].ToString()));
                            break;                        
                    }
                                        
                }                
                catch (InvalidSyncStateException e)
                {
                    throw;
                }
                catch (UnknownSyncStateException e)
                {
                    throw;
                }
                catch (Exception e)
                {
                    throw new Exception (string.Format("Failed to convert the sync state value {0}, for the attribute mapping for the attribute {1} between the master Entity {2} and the slave Entity {3}."
                                                  ,this.MasterAttribute.Name
                                                  ,MasterToSlaveAttributeMappingData["AttributeSyncState"].ToString()
                                                  ,MasterToSlaveAttributeMappingData["MasterEntity_Name"].ToString()
                                                  ,MasterToSlaveAttributeMappingData["SlaveEntity_Name"].ToString()), e);
                }
            }
            else
            {   
                throw new NotSupportedQueryVersionException(string.Format("Query Version {0} is not supported.", QueryVersion.ToString()));
            }

        }


        public override string ToString()
        {
            StringBuilder str = new StringBuilder();

            //sb.AppendLine(string.Format("Mapping Code {0}", this.MappingCode));
            str.AppendLine("Master" + MasterAttribute.ToString() + " to " + "Slave" + SlaveAttribute.ToString() + " AttributeSyncState:" + AttributeSyncState.ToString());
            
            return str.ToString();
        }


    }
}
